package logmanager.view_connection;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import logmanager.proxy.ServerLogManager;
import logmanager.proxy.ServerLogManagerServiceLocator;
import logmanager.view.Principal;

//classe d'action de connection
public class ActionConnection implements MouseListener {
	public ServerLogManager logManager;
	public Principal p;
	public JTextField wsHost, wsPort;

	public ActionConnection(Principal p, ServerLogManager logManager, JTextField wsHost, JTextField wsPort) {
		this.p = p;
		this.logManager = logManager;
		this.wsHost = wsHost;
		this.wsPort = wsPort;
	}

	public void mouseClicked(MouseEvent arg0) {
		if (System.getSecurityManager() == null)
			System.setProperty("java.security.policy", "setting/allpolicy.txt");
		ServerLogManagerServiceLocator sl = new ServerLogManagerServiceLocator();
		sl.setConnectionParams(wsHost.getText(), wsPort.getText());
		try {
			ServerLogManager logManager = sl.getServerLogManager();	
			if(logManager.connect()){
				if(logManager == null)
					System.out.println("est null");
				p.connected(logManager);
			}else{
				JOptionPane.showMessageDialog(p, "Erreur de reponse du serveur","Erreur",JOptionPane.ERROR_MESSAGE);
			}
		} catch (Exception e) {
			System.out.println("connection : " + e.getMessage());
			JOptionPane.showMessageDialog(p, "Erreur de connection au serveur","Erreur",JOptionPane.ERROR_MESSAGE);			
			return;
		}

	}

	public void mouseEntered(MouseEvent arg0) {}

	public void mouseExited(MouseEvent arg0) {}

	public void mousePressed(MouseEvent arg0) {}

	public void mouseReleased(MouseEvent arg0) {}

}
